/****************************************************************************/
/*This program merges the tornado and business data together and            */
/*prepares it for the regression program                                    */
/****************************************************************************/
 

#delimit;
clear;
set mem 1200m;
set matsize 800;
set more off;
capture log close;
set trace off;

/****************************************************************************/
/* DEFINE THE PROJECT FOLDER                                                */
/****************************************************************************/

*local dropboxpath C:\Users\srohlin\Dropbox\Tornadoes;
local pathsmr  C:\Research;

/****************************************************************************/
/*DEFINE PATH TO FOLDERS                                                    */
/****************************************************************************/

local Torpath  `pathsmr'\Tornadoes\0515Miles_CountyM;

/****************************************************************************/
/*Open tornado data to cut the business data before the fillin command      */
/****************************************************************************/

use `Torpath'\Data\tornado_data_cleaned_0515MilesBuffer.dta, clear;

capture drop _merge;

sort bgfips tornado_id;
by bgfips: gen temp = _n;

keep if temp == 1;

save `Torpath'\Temp\tornado_data_temp_allinfo_1.dta, replace;

keep bgfips temp;

sort bgfips;
save `Torpath'\Temp\tornado_data_temp_1.dta, replace;

/****************************************************************************/
/*Open business data                                                        */
/*Even though it says wide panel it means in relation to industry not year  */
/****************************************************************************/

use `Torpath'\Data\bgfips_level_allyears_wide_final_sizev2.dta, clear;
sort bgfips year;

merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_1.dta;
tab _merge;    /*We have some 2s which means that some blocks don't have businesses which is fine*/
keep if _merge == 3;
drop _merge temp;

/****************************************************************************/
/*Use fillin command to create a balanced panel                             */
/****************************************************************************/

drop if year ==.;

sort year bgfips;
capture fillin year bgfips;

/****************************************************************************/
/*Merge on tornado data to add information to the filled in data            */
/****************************************************************************/

sort bgfips year;
merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_allinfo_1.dta;
tab _merge;

keep if _merge == 3;
drop _merge;

sort bgfips;
save `Torpath'\Temp\businesssizev2_tornado_data_1.dta, replace;


//Some blocks experienced mor ethan one tornadoes

use `Torpath'\Data\tornado_data_cleaned_0515MilesBuffer.dta, clear;

capture drop _merge;

sort bgfips tornado_id;
by bgfips: gen temp = _n;

keep if temp == 2;

save `Torpath'\Temp\tornado_data_temp_allinfo_2.dta, replace;

keep bgfips temp;

sort bgfips;
save `Torpath'\Temp\tornado_data_temp_2.dta, replace;


/****************************************************************************/
/*Open business data                                                        */
/*Even though it says wide panel it means in relation to industry not year  */
/****************************************************************************/

use `Torpath'\Data\bgfips_level_allyears_wide_final_sizev2.dta, clear;

/****************************************************************************/
/*Merge on tornado data first to reduce file size                           */
/****************************************************************************/

sort bgfips year;

merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_2.dta;
tab _merge;    /*We have some 2s which means that some blocks don't have businesses which is fine*/
keep if _merge == 3;
drop _merge temp;


/****************************************************************************/
/*Use fillin command to create a balanced panel                             */
/****************************************************************************/

drop if year ==.;

sort year bgfips;
capture fillin year bgfips;

/****************************************************************************/
/*Merge on tornado data to add information to the filled in data            */
/****************************************************************************/

sort bgfips year;
merge m:1 bgfips using `Torpath'\Temp\tornado_data_temp_allinfo_2.dta;
tab _merge;

keep if _merge == 3;
drop _merge;

append using `Torpath'\Temp\businesssizev2_tornado_data_1.dta;

/****************************************************************************/
/*To keep file small we drop the years we currently are not using           */
/*This is currently based on 12 tornados using 3 years before and after     */
/****************************************************************************/

tab tornado_year;

/****************************************************************************/
/*Replace missings with zeros                                               */
/****************************************************************************/

global outcomestubs "firm_lt3 emp_lt3 sales_lt3 firm_lt10 emp_lt10 sales_lt10 firm_gt3lt7 emp_gt3lt7 sales_gt3lt7 firm_gt7 emp_gt7 sales_gt7 firm_gt10 emp_gt10 sales_gt10" ;
global industrystubs "all rs" ;


foreach i of global outcomestubs { ;
foreach j of global industrystubs { ;
replace `i'_`j' = 0 if `i'_`j' ==. ;
};
};

/****************************************************************************/
/*save cleaned tornado data                                                 */
/****************************************************************************/

sort bgfips;
save `Torpath'\Data\business_tornado_data_final_sizev2.dta, replace;




